<template>
	<view class="indextop">
		<zbar></zbar>
		<view class="search flex">
			<view class="search_flex" @click="$pageGo('/pages/index/search')">
				<uni-icons color="#A3A3A3" size="20" type="search"></uni-icons>
				<view class="" style="margin-left: 10rpx;">
					今日特价
				</view>
			</view>
		</view>
	</view>
	<!-- 	<view class="gg">
		<uni-notice-bar showIcon scrollable single text="123123"></uni-notice-bar>
	</view> -->
	<view class="notice">
		<view class="notice_img">
			<image src="/static/lb.png" mode=""></image>
		</view>
		<swiper :indicator-dots="false" :autoplay="true" :interval="5000" :duration="2000" :circular="true"
			:disable-touch="true" :vertical="true" class="notice_center">
			<swiper-item v-for="v in noticeList" :key="v.id" @click="toLook(v)">
				{{v.title}}
			</swiper-item>
		</swiper>
	</view>

	<swiper class="swiper" indicator-color='#FFFFFF' indicator-active-color='#FF7700' circular indicator-dots autoplay
		:duration="500">
		<swiper-item v-for="(item,index) in bannerList" :key="index">
			<image :src="item.img" mode="aspectFill"></image>
		</swiper-item>
	</swiper>

	<view class="scroll_s">
		<scroll-view class="scroll" :scroll-x="true">
			<view class="block_flex">
				<view class="items" v-for="(item,index) in categoryList" :key="index" @click="goclassification(item)">
					<image class="img" :src="item.icon" mode="aspectFill"></image>
					<view class="text">
						{{item.name}}
					</view>
				</view>
			</view>
		</scroll-view>
	</view>

	<view class="shoptitle">
		推荐商品
	</view>
	<view class="goods" v-if="data.shopList.length!=0">
		<custom-waterfalls-flow :value="data.shopList" @imageClick='godetail'>
			<view class="itemss" v-for="(item,index) in data.shopList" :key="index" @click.stop='godetail(item)'
				slot="slot{{index}}">
				<view class="title">{{item.name}}</view>
				<view class="item_flex">
					<view class="price">
						<text>￥</text>{{item.price}}<text class="t2">/{{item.unit}}</text>
					</view>
					<view @click.stop="" class="">
						<countNum v-model.number="item.carCount" :shop='item'></countNum>
					</view>
				</view>
			</view>
		</custom-waterfalls-flow>
	</view>
	<uv-tabbar :value="0" class="tab">
	</uv-tabbar>
</template>

<script setup>
	import {
		useStore
	} from 'vuex';
	import {
		onLoad,
		onShow,
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		reactive,
		ref
	} from 'vue';
	import {
		login,
		getbanner,
		gettips,
		getcategory,
		getshoplist,
		getShopCar
	} from '@/common/http.api.js';

	onLoad(() => {
		getlogin()
	})
	// onShow(()=>{
	// 	getShoplistsssss()
	// })
	onReachBottom(() => {
		data.params.pageNum++
		getShoplistsssss()
	})
	const noticeList = ref([])

	function getTips() {
		gettips({}).then(res => {
			noticeList.value = res.data
		})
	}

	function toLook(item) {
		uni.navigateTo({
			url: '/pages/index/ggdetail?item=' + encodeURIComponent(JSON.stringify(item))
		})
	}
	//////////////////////////////////
	const bannerList = ref([])

	function getBanner() {
		getbanner({}).then(res => {
			bannerList.value = res.data
		})
	}
	//////////////////////////
	const categoryList = ref([])

	function getCategory() {
		getcategory({
			parentId: 0
		}).then(res => {
			categoryList.value = res.data
		})
	}

	const data = reactive({
		params: {
			pageNum: 1,
			pageSize: 10
		},
		shopList: []
	})

	function getShoplistsssss() {
		getshoplist(data.params).then(res => {
			data.shopList.push(...res.rows)
		})
	}

	function goclassification(item) {
		uni.setStorageSync('category_id', item.id)
		uni.switchTab({
			url: `/pages/classification/classification`
		})
	}


	function getCarCount() {
		getShopCar({}).then(res => {
			if (res.code == 200) {
				let total = res.data.reduce((sum, e) => sum + Number(e.productCount || 0), 0)
				store.commit('setShopNum', total)
			}
		})
	}

	function getlogin() {
		uni.login({
			provider: 'weixin',
			success: function(loginRes) {
				login({
					code: loginRes.code
				}).then(res => {
					if (res.code == 200) {
						uni.setStorageSync('goodToken', res.data.token)
						uni.setStorageSync('userInfo', res.data.user)
						getCarCount()
						getTips()
						getBanner()
						getCategory()
						getShoplistsssss()
					}
				})
			}
		});
	}
	const store = useStore();

	function godetail(item) {
		uni.navigateTo({
			url: `/pages/index/shopdetail?shopid=${item.id}`
		})
	}
</script>
<script>
	export default {
		onShareAppMessage() {
			return {
				title: '欢迎使用集优送！',
				path: `pages/index/index`,
			}
		},
		onShareTimeline() {
			return {
				title: '欢迎使用集优送！',
				path: `pages/index/index`,
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #F8F8F8;
	}

	.indextop {
		width: 750rpx;
		background: linear-gradient(180deg, #FF7700 0%, #FFE396 56%, #FFE396 100%);
		position: sticky;
		top: 0;
		z-index: 9;

		.search {
			width: 750rpx;
			height: 88rpx;

			.search_flex {
				margin-left: 20rpx;
				display: flex;
				align-items: center;
				width: 520rpx;
				height: 64rpx;
				background: #FFFFFF;
				border-radius: 112rpx 112rpx 112rpx 112rpx;
				padding: 0rpx 20rpx;
				box-sizing: border-box;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #A3A3A3;
			}
		}
	}

	.notice {
		width: 710rpx;
		height: 70rpx;
		background: #FFFFFF;
		border-radius: 10rpx;
		margin: 20rpx auto;
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
		padding: 32rpx 30rpx;
		overflow: hidden;

		.notice_img {
			width: 36rpx;
			height: 36rpx;
			margin-right: 21rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.notice_center {
			flex: 1;
			height: 100rpx;
			line-height: 100rpx;
			font-weight: 500;
			font-size: 26rpx;
			color: #666666;
		}
	}

	.swiper {
		width: 100%;
		width: 710rpx;
		height: 280rpx;
		margin: 0rpx auto;

		image {
			width: 710rpx;
			height: 280rpx;
			border-radius: 20rpx;
		}
	}

	.scroll_s {
		margin: 30rpx auto 30rpx;

		.block_flex {
			display: flex;
			// height: 300rpx;
			flex-wrap: wrap;

			.items {
				width: 150rpx;
				text-align: center;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				box-sizing: border-box;
				margin-bottom: 10rpx;

				.text {
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 28rpx;
					color: #7E7E7E;
					margin-top: 10rpx;
				}

				.img {
					width: 90rpx;
					height: 90rpx;
					border-radius: 50rpx;
				}
			}
		}
	}


	.shoptitle {
		margin-left: 20rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: bold;
		font-size: 32rpx;
		color: #000000;
	}

	.shoptitle:before {
		content: '|';
		color: #FF7700;
	}

	.goods {
		width: 710rpx;
		margin: 30rpx auto;

		.itemss {
			padding: 20rpx;
			box-sizing: border-box;

			.title {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 32rpx;
				color: #000000;
			}

			.item_flex {
				.price {
					font-family: PingFang SC, PingFang SC;
					font-weight: bold;
					font-size: 32rpx;
					color: #FF0000;

					text {
						font-weight: 500;
						font-size: 20rpx !important;
					}

					.t2 {
						font-weight: 500;
						font-size: 24rpx !important;
						color: #efefef;
					}
				}

				margin-top: 10rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
			}
		}
	}
</style>